﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XMLSpreadGear.Core
{
    public class MultiElement<T>:BaseElement
        where T: BaseElement
    {
        IList<T> listelement;
        public MultiElement(string strLocalName)
            : base(strLocalName)
        {
            listelement = new List<T>();
        }

        public void Add(T item)
        {
            this.listelement.Add(item);
        }

        public T this[int i]
        {
            get { return this.listelement[i]; }
            set
            {
                this.listelement.Insert(i, value);
            }
        }
        internal protected override IDictionary<string, string> RegisterElements()
        {
            throw new NotImplementedException();
        }

        internal protected override IDictionary<string, string> RegisterAttributes()
        {
            throw new NotImplementedException();
        }

        public override System.Xml.XmlDocument GetElement()
        {
            XmlDocument xdock = new XmlDocument();
            XmlNode root = xdock.CreateNode(XmlNodeType.Element, "Root", string.Empty);

            foreach (T item in listelement)
            {
                XmlNode thisnode = xdock.CreateNode(XmlNodeType.Element, this.LocalName, string.Empty);

                IDictionary<string, string> elements = item.RegisterElements();
                IDictionary<string, string> attributes = item.RegisterAttributes();

                if (attributes.Count > 0)
                {
                    for (int i = 0; i < attributes.Count; i++)
                    {
                        //XmlAttribute attr = xdock.CreateAttribute(attributes.Keys.ElementAt(i));
                        XmlAttribute attr = GenerateAttribute(xdock, attributes.Keys.ElementAt(i));
                        attr.Value = attributes[attributes.Keys.ElementAt(i)];
                        thisnode.Attributes.Append(attr);
                    }
                }

                if (elements.Count > 0)
                {
                    for (int i = 0; i < elements.Count; i++)
                    {
                        //XmlNode element = xdock.CreateElement(elements.Keys.ElementAt(i));
                        //XmlNode element = xdock.CreateNode(XmlNodeType.Element, elements.Keys.ElementAt(i), string.Empty);
                        //element.InnerText = elements[element.Name];
                        //thisnode.AppendChild(element);
                        thisnode.InnerXml = thisnode.InnerXml + elements[elements.Keys.ElementAt(i)];
                    }
                }

                root.InnerXml = root.InnerXml + thisnode.OuterXml;

            }
            xdock.AppendChild(root);
            return xdock;
        }

        public override string ToString()
        {
            XmlNode node = GetElement().GetElementsByTagName("Root").OfType<XmlNode>().FirstOrDefault();
            if (node != null)
            {
                return node.InnerXml;
            }
            else
                return string.Empty;
        }

        protected override void SetDefaults()
        {
            return;
        }
    }
}
